/***************************************
  PREDAJA PODATAKA O NOVOM KOMITENTU
  SKRIPTI KOJA VRŠI UPIS U BAZU
***************************************/
function addNewKomitent(d){
	saveData();
	/*if(data.check()){
		data.send();
	}*/
	data.send();
}
function deleteKomitent(id,next,prev){
	//alert(prev+"->"+id+"->"+next);
	if(next == id){
		if(prev == id){
			next = "0";
		}else{
			next = prev;
		}
	}
	if(confirm("Potvrdite brisanje?")){
		LoadRSDoc("komitenti.php?a=del&id="+id,"afterDelete('"+next+"')");
	}
}
function afterDelete(response,next){
	alert(response);
	if(next){
		LoadRSDoc("komitenti.php?a=displayOne&id="+next,"setContent");
	}else{
		LoadRSDoc("index.php","setContent");
	}
}
function saveKomitent(){
	saveData();
	ret = data.check();
	if(ret){
	//if(1){	//cuvanje bez provere
		data.send();
	}
}
function adminKomitentTypes(action,type){
	var ul = document.getElementById("komitenti_types");
	var inpts = ul.getElementsByTagName("input");
	var labels = ul.getElementsByTagName("label");
	var dels = Array();
	var out = "";
	var ids = "";
	for(i=0; i < inpts.length; i++){
		if((inpts[i].type == "checkbox") && (inpts[i].checked)){
			var id = inpts[i].name.substr(8);
			ids = ids + ","+id;
			out = out + "- " + labels[i].innerHTML+"\n";
		}
	}
	ids = ids.substr(1);
	if(!ids){
		alert("Niste ništa označili");
		return;
	}
	switch(type){
		case 'type':
			var delA = 'delType';
			var delFunc = "prikazTipova";
			var edA = 'editType';
			var edFunc = "setContent";
			break;
		case 'struct':
			var delA = 'delStruct';
			var delFunc = "prikazStruct";
			var edA = 'editStruct';
			var edFunc = "setContent";
			break;
	}
	switch(action){
		case 'delete':
			if(confirm("Da li ste sigurni u vezi brisanja?\n\n"+out+"\n")){
				LoadRSDoc("komitenti.php?a="+delA+"&id="+ids,delFunc);
			}
			break;
		case 'edit':
			LoadRSDoc("komitenti.php?a="+edA+"&id="+ids,edFunc);
			break;
	}
}
function newKomitentType(forma,what){
	var new_val = forma.kom_type_name.value;
	if(new_val){
		switch(what){
			case 'type':
				LoadRSDoc("komitenti.php?a=addNewType&t=type&name="+new_val,"prikazTipova");
				break;
			case 'struct':
				LoadRSDoc("komitenti.php?a=addNewType&t=struct&name="+new_val,"prikazStruct");
				break;
		}
	}
}
function prikazTipova(str){
	if(str){
		alert(str);
	}
	LoadRSDoc("komitenti.php?a=types","setContent");
}
function prikazStruct(str){
	if(str){
		alert(str);
	}
	LoadRSDoc("komitenti.php?a=struct","setContent");
}
function updateKomitentType(forma,tip){
	var a;
	switch(tip){
		case 'types':
			a = "updateTypes";
			break;
		case 'struct':
			a = "updateStructs";
			break;
	}
	
	var els = forma.elements;
	params = "";
	var num=0;
	for(i=0; i<els.length; i++){
		if((els[i].nodeName.toLowerCase()=="input") && (els[i].type.toLowerCase()=="text")){
			var id = els[i].name.substr(5);
			value = els[i].value;
			params = params + id+"{!!}"+value+"{!}";
			num++;
		}
	}
	params = params.substr(0,params.length-3);
	LoadRSDoc("komitenti.php?a="+a+"&pars="+params,"setContent");
}
function toggleAllTypes(obj){
	var sviLabel = document.getElementById("sviLabel");
	if(obj.checked){
		var v = true;
		sviLabel.innerHTML = "deselektuj";
	}else{
		var v = false;
		sviLabel.innerHTML = "selektuj sve";
	}
	var ul = document.getElementById("komitenti_types");
	var inpts = ul.getElementsByTagName("input");
	for(i=0; i<inpts.length; i++){
		inpts[i].checked = v;
	}
}
function checkKeywords(txt){
	var status = document.getElementById("keywords_status");
	var val = txt.value;
	if(!val){
		status.innerHTML = "";
		return;
	}
	var lines = val.split("\n");
	var arr = Array();
	for(var i=0; i<lines.length; i++){
		if(lines[i].length){
			var line = lines[i];
			line = line.replace(/^\s*|\s*$/g,"");
			line = line.replace(/\s{2,}/g," ");
			arr.push(line);
		}
	}
	arr = unique(arr);
	if(arr.length < lines.length){
		status.className = "error";
		status.innerHTML = "Duplikata: "+(lines.length-arr.length);
		var n = implode("\\n",arr);
		status.innerHTML += '<input type="button" onclick="document.getElementById(\''+txt.id+'\').value=\''+n+'\';this.parentNode.className=\'\'; this.parentNode.innerHTML=\'Ukupno: '+arr.length+'\'" value="Ukloni" />';
	}else{
		status.innerHTML = "Ukupno: " + lines.length;
		txt.value="";
		for(i=0; i<arr.length; i++){
			if(i) txt.value += "\n";
			txt.value += arr[i];
		}
	}
}
function unique(arr){
	var a = Array(), i, l = arr.length;
	for( i=0; i<l; i++ ) {
		if( a.indexOf( arr[i] ) < 0 ){
			a.push( arr[i] ); 
		}
	}
	return a;
}
function implode(glue,a){
	var ret="";
	if(typeof(a) == "object"){
		for(var i=0; i<a.length; i++){
			ret = ret + a[i];
			if(i < (a.length - 1))
				ret += glue;
		}
		return ret;
	}else{
		return false;
	}
}
function removeElement(obj){
	obj.parentNode.removeChild(obj);
}
function removeElements(id){
	var el = document.getElementById(id);
	var lis = el.getElementsByTagName("li");
	for(var i=0; i<lis.length; i++){
		var li = lis[i];
		if(li.className == "selected"){
			el.removeChild(li);
			i--;
		}
	}
}
function removeAllElements(id){
	var obj = document.getElementById(id);
	for(var i=0; i<obj.childNodes.length; i++){
		var el = obj.childNodes[i];
		obj.removeChild(el);
		i--;
	}
}
function addElement(from,to){
	var f = document.getElementById(from);
	var test = testElement2(f);
	if(!f.value || !test){
		alert("Uneti podaci nisu validni.");
		f.focus();
		f.select();
		return;
	}
	var t = document.getElementById(to);
	var elem = document.createElement("li");
	elem.innerHTML = f.value;
	elem.ondblclick = function(){
		 removeElement(this);
	}
	elem.onclick = function(){
		this.className = this.className?null:"selected";
	}
	t.appendChild(elem);
	f.select();
}
function duplicateElement(elem){
	var obj = elem.parentNode;
	var remBtn = elem.cloneNode(false);
	remBtn.value = "-";
	remBtn.onclick = function(){
		this.parentNode.parentNode.removeChild(this.parentNode);
	}
	var klasa = obj.className;
	var row = document.createElement("div");
	var reps = obj.parentNode.getElementsByTagName("div");
	var next = getNextId(reps,klasa);
	var toRepeat = getRepeat(reps);
	row.id = next;
	row.innerHTML = toRepeat;
	row.className = klasa;	
	obj.parentNode.appendChild(row);
	row.appendChild(remBtn);
}
function getNextId(arr,klasa){
	var redovi = Array();
	var idejevi = Array();
	for(var i=0; i<arr.length; i++){
		if(arr[i].className == klasa){
			redovi.push(arr[i])
			idejevi.push(arr[i].id?arr[i].id:0);
		}
	}
	idejevi.sort();
	return parseInt(idejevi[idejevi.length-1])+1;
}
function getRepeat(obj){
	var ret;
	var tmp = obj[0].getElementsByTagName("div");
	for(var i=0; i<tmp.length; i++){
		if(tmp[i].className == "repeat"){
			ret = tmp[i].innerHTML;
			break;
		}
	}
	return ret;
}
var dels=Array();
function komitentAddDelatnost(btn){
	var err = document.error;
	if(err.length){
		alert("Prvo ispravite greške");
		return;
	}
	
	var hldr = btn.parentNode.parentNode;
	var delData = document.getElementById("komitent_delatnosti_data");
	var sel = hldr.getElementsByTagName("select");
	sel = sel[0];
	var del_name = sel.options[sel.selectedIndex].innerHTML;
	var del_id = sel.value;
	var len=0;
	if(del_id>0){
		var ul = hldr.getElementsByTagName("ul");
		ul = ul[0];
		var lis = ul.getElementsByTagName("li");
		var len = lis.length;
		var ima = false;
		for(var i=0; i<len; i++){
			if(lis[i].id == del_id){
				ima = true;
			}
		}
		if(!ima){
			for(var i=0; i<len; i++){
				lis[i].className = "";
			}
			
			// creating new del list item
			var li = document.createElement("li");
			li.id = del_id;
			li.className = "current";
			li.innerHTML = '<span class="delItem" style="width:25px;">'+(del_id)+'</span><span class="delItem">-&nbsp;'+del_name+'</span><input type="checkbox" name="glavna_'+del_id+'" value="1" />';
			ul.appendChild(li);
			li.onclick = function(){
				showDataForDelatnost(this);
			}
			
			// creating del_data
			LoadRSDoc("komitenti.php?a=newDelHTML&id="+del_id,"komitentAddDelatnost1("+del_id+")");
			/*var tpl = document.getElementById("template");
			var tmp = tpl;
			var tplCopy = tpl.cloneNode(true);
			
			tplCopy.id = del_id;
			tplCopy.setAttribute("delid",del_id);
			tplCopy.style.display = "block";
			var tagNames = Array("input","textarea","select","label");
			for(var i=0; i<tagNames.length; i++){
				var tags = tplCopy.getElementsByTagName(tagNames[i]);
				for(var j=0; j<tags.length; j++){
					if(tags[j].nodeName.toLowerCase() != "label"){
						var ime = tags[j].name;
						if(ime.length){
							tags[j].name = ime+"_"+tplCopy.id;
							tags[j].id = ime+"_"+tplCopy.id;
						}
					}else{
						var ime = tags[j].getAttribute("for");
						tags[j].setAttribute("for",ime+"_"+tplCopy.id);
					}
				}
			}
			delData.appendChild(tplCopy);*/
		}
		doTheRules();
	}
}
function komitentAddDelatnost1(template,del_id){
	var del_data = document.getElementById("komitent_delatnosti_data");
	var bq = del_data.getElementsByTagName("blockquote");
	/*var ss = document.createElement("span");
	ss.innerHTML = template
	del_data.appendChild(ss);*/
	del_data.innerHTML += template;
	saveData();
	komitentDisplayDelatnostData(del_id);
}
function komitentRemoveDelatnost(){
	var minDel = 1;
	var delsHldr = document.getElementById("komitent_delatnosti");
	var dels = delsHldr.getElementsByTagName("li");
	
	var ugodels = delsHldr.getAttribute("ugovor_dels");
	var ugodat = delsHldr.getAttribute("ugovor_dels_dat");
	var ugobroj = delsHldr.getAttribute("ugovor_dels_broj");
	var ugostavke = delsHldr.getAttribute("ugovor_dels_stavke");
	ugostavke = ugostavke.replace(/\[t\]/gi,"\t");
	ugostavke = ugostavke.replace(/\[n\]/gi,"\n");
	
	if(dels.length > (minDel+1)){
		for(var i=0; i<dels.length; i++){
			if(dels[i].className == "current"){
				
				if(ugodels.match(";"+dels[i].id+";")){
					var msg = "Na ugovoru #"+ugobroj+" od "+ugodat;
					msg += " postoji stavka u odabranoj delatnosti ili link ka istoj!"
					msg += "\n\nSadržaj ugovora:"+ugostavke;
					msg += "\n\nDa li ipak želite da obrišete odabranu delatnost?";
					if(!confirm(msg)){
						return;
					}
				}
				
				komitentRemoveDelatnostData(dels[i].id);
				delsHldr.removeChild(dels[i]);
				saveData();
				break;
			}
		}
	}else{
		alert("Komitent mora imati barem "+minDel+" delatnost.");
	}
}
function komitentRemoveDelatnostData(delid){
	var delsDataHldr = document.getElementById("komitent_delatnosti_data");
	var fsets = delsDataHldr.getElementsByTagName("blockquote");
	for(var i=0; i<fsets.length; i++){
		if(fsets[i].getAttribute("delid") == delid){
			if(fsets[i].parentNode.id != "komitent_delatnosti_data"){
				delsDataHldr.removeChild(fsets[i].parentNode);
			}else{
				delsDataHldr.removeChild(fsets[i]);
			}
		}
	}
}
function showDataForDelatnost(obj){
	if(document.error.length){
		imaGresaka("Prvo ispravite greške!");
		return;
	}
	var lis = obj.parentNode.getElementsByTagName("li");
	for(var i=0; i<lis.length; i++){
		if(lis[i] == obj){
			lis[i].className="current";
		}else{
			lis[i].className="";
		}
	}
	komitentDisplayDelatnostData(obj.id);
}
function komitentDisplayDelatnostData(id){
	var delData = document.getElementById("komitent_delatnosti_data");
	var bqs = delData.getElementsByTagName("blockquote");
	var ima = false;
	//var maxID=0;
	var dbg = ""
	for(var i=0; i<bqs.length; i++){
		var bq = bqs[i];
		var delid = bq.getAttribute("delid");
		if(delid == id){
			bq.style.display = "block";
			var theBq = bq;
			ima = true;
		}else{
			bq.style.display = "none";
		}
	}
	doTheRules();
}
function komitentAddCard(btn){
	var div = btn.parentNode;
	var ul = div.getElementsByTagName("ul");
	ul = ul[0];
	var sel = div.getElementsByTagName("select");
	sel = sel[0];
	var selSel = sel.options[sel.selectedIndex];
	
	var lis = ul.getElementsByTagName("li");
	var ima = false;
	if(lis.length){
		for(var i=0; i<lis.length; i++){
			if(lis[i].value == selSel.value){
				ima = true;
				//alert("Kartica \""+lis[i].innerHTML+"\" je već dodata.");
			}
		}
	}
	if(!ima){
		var li = document.createElement("li");
		li.value = selSel.value;
		li.innerHTML = selSel.innerHTML;
		li.onclick = function(){
			if(this.className == "selected"){
				this.className = "";
			}else{
				this.className = "selected";
			}
		}
		li.ondblclick = function(){
			this.parentNode.removeChild(this);
		}
		ul.appendChild(li);
	}
}
function komitentRemoveCard(btn){
	var div = btn.parentNode;
	var ul = div.getElementsByTagName("ul");
	ul = ul[0];
	var lis = ul.getElementsByTagName("li");
	var forRemove = Array();
	var l = lis.length;
	for(var i=0; i<l; i++){
		if(lis[i].className == "selected"){
			forRemove.push(lis[i]);
		}
	}
	if(!forRemove.length){
		//alert("Nema označenih kartica za uklanjanje.\nPrvo označite željenu karticu.");
	}else{
		for(x in forRemove){
			ul.removeChild(forRemove[x]);
		}
	}
}
function setPtt(pttID,sel,value){
	if(value){
		if(pttID){
			pttObj = document.getElementById(pttID);
		}else{
			var hldr = sel.parentNode;
			var ptts = getElementsByClassName(hldr,"ptt");
			pttObj = ptts[0];
		}
		pttObj.innerHTML = value;
	}
}
function delatnost(id,name,hldr){
	var h = hldr.parentNode;
	var fsets = h.getElementsByTagName("fieldset");
	this.address = Array();
	this.id = id;
	this.name = name;
	for(x in fsets){
		var s = new set(fsets[x],x);
		this.address.push(s);
	}
}
function set(fset,x){
	this.regid = x;
	this.mesid;
	this.opsid;
	this.nasid;
	this.ulica;
	this.broj;
	this.phones = Array();
	this.faxes = Array();
	this.emails = Array();
	this.webs = Array();
}
function saveData(){
	data = new basic_data();
}
function setDefaultDelatnost(li){
	var delID = li.id;
	var delNAME = li.innerHTML.split("</span>");
	var delNAME = delNAME[1];
	setOsnDel(delID,delNAME);
}
function setDefaultDelatnostUL(id){
	var ul = document.getElementById(id);
	if(ul){
		var li=null;
		var lis = ul.getElementsByTagName("li");
		for(var i=0; i<lis.length; i++){
			if(lis[i].className == "current"){
				li = lis[i];
				break;
			}
		}
		if(li){
			setDefaultDelatnost(li);
		}
	}
}
function updateSelect(str,id,par){
	var hldr;
	if(par.length){
		var hldr = document.getElementById(par);
	}else{
		var hldr = document.getElementById(id).parentNode;
	}
	var sels = hldr.getElementsByTagName("select");
	var sel=null;
	for(var i=0; i<sels.length; i++){
		var rgx = new RegExp("^"+id);
		if(rgx.test(sels[i].id)){
			sel = sels[i];
			break;
		}
	}
	if(!sel){
		alert("Nema select-a sa ID="+id+" u objektu ID="+par);
		return;
	}
	sel.innerHTML = '<option value="-1">---</option>';
	if(str.length){
		var tmp = str.split("{!!}");
		for(var i=0; i<tmp.length; i++){
			var t = tmp[i].split("{!}");
			var opt = document.createElement("option");
			switch(t.length){
				case 2:
					opt.value = t[0];
					opt.innerHTML = t[1];
					break;
				case 3:
					opt.value = t[0];
					opt.innerHTML = t[1];
					opt.ptt = t[2];
					break;
			}
			sel.appendChild(opt);
		}
	}else{
		sel.innerHTML = '<option value="-1">---</option>';
	}
}
function resetSelect(obj,elem_ids){
	var holder = obj.parentNode;
	var t = typeof(elem_ids);
	if(t.toLowerCase() == "string"){
		elem_ids = Array(elem_ids);
	}
	var sels = holder.getElementsByTagName("select");
	var out = "";
	for(var i=0; i<sels.length; i++){
		var s = inArray(elem_ids,sels[i].id);
		if(s !== false){
			sels[i].innerHTML = '<option value="-1">---</option>';
		}
	}
}
function test(){
	if(dels){
		var out="";
		for(x in dels){
			var obj = dels[x];
			out += typeof(obj)+": ";
			if(typeof(obj) == "object"){
				for(y in obj){
					out += "   "+y+"="+obj[y]+", "
				}
			}
			out+="\n";
		}
		//alert(out);
	}else{
		alert("Empty")
	}
}
/*function addKomdelData(btn,delid){
	var i,tmpindex;
	var fset = btn.parentNode.parentNode;
	var index=0;
	var fsets = fset.parentNode.getElementsByTagName("fieldset");
	var newFset = fset.cloneNode(true);
	for(i=0; i<fsets.length; i++){
		tmpindex = fsets[i].id.substr(fsets[i].id.lastIndexOf("_")+1);
		if(tmpindex>index){
			index=tmpindex;
		}
	}
	index++;
	injectObject = fset.parentNode;
	setFsetIndexes(newFset,index);
	injectObject.appendChild(newFset);
}*/

var injectObject=null;
function addKomdelData(btn,delid){
	var i,tmpindex;
	var fset = btn.parentNode.parentNode;
	
	if(fset.nextSibling){
		var n = fset.nextSibling.nodeName.toLowerCase();
		if(n=="#text"){
			n = fset.nextSibling.nextSibling
		}else{
			n = fset.nextSibling;
		}
	}else{
		n=null;
	}
	if(n==null){
		var fsid = fset.id;
		var tmpindex = fsid.substr(fsid.lastIndexOf("_")+1);
		index=tmpindex>0?tmpindex:0;
		index++;
		injectObject = fset.parentNode;
		LoadRSDoc("komitenti.php?a=duplicateKomdelData&i="+index+"&d="+delid,"injectKomdelData");
	}
}
function injectKomdelData(html){
	injectObject.innerHTML += html;
	injectObject = null;
	doTheRules();
}
function removeKomdelData(btn){
	var fset = btn.parentNode.parentNode;
	var h = fset.parentNode;
	var fsets = h.getElementsByTagName("fieldset");
	var www = fset.getAttribute("www");
	if(fsets.length > 1){
		if(confirm("Da li ste sigurni da želite da izbrišete podatke?")){
			if(trim(www)>0){
				if(confirm("Na ove podatke su vezani podaci za internet!\nDa li ste sigurni da želite da ih obrišete?")){
					//alert("Brisanje ovnih podataka nije dozvoljeno.\nNa ove podatke su vezani podaci za internet.");
					fset.parentNode.removeChild(fset);
				}
			}else{
				fset.parentNode.removeChild(fset);
			}
		}
	}
}

function duplicateFset(btn){
	var hld = btn.parentNode.parentNode.parentNode;
	var fset = btn.parentNode.parentNode;
	var fsets = fset.parentNode.getElementsByTagName("fieldset");
	var newFset = fset.cloneNode(true);
	var spans = newFset.getElementsByTagName("span");
	if(spans.length){
		for(var i=0; i<spans.length; i++){
			if(spans[i].className == "ptt"){
				spans[i].innerHTML = '';
			}
		}
	}
	var lengths = fset.parentNode.getElementsByTagName("input");
	var length = lengths[0].value;
	length++;
	lengths[0].value = length;
	var inputs = newFset.getElementsByTagName("input");
	var selects = newFset.getElementsByTagName("select");
	var textareas = newFset.getElementsByTagName("textarea");
	var labels = newFset.getElementsByTagName("label");
	var toIncrement = Array(inputs,selects,textareas,labels,Array(newFset));
	for(var i=0; i<toIncrement.length; i++){
		var obj = toIncrement[i];
		for(var x=0; x<obj.length; x++){
			if(obj[x].nodeName.toLowerCase() == "label"){
				var f = obj[x].getAttribute("for");
				obj[x].setAttribute("for",f.substr(0,f.lastIndexOf("_"))+"_"+length);
			}else if(obj[x].nodeName.toLowerCase() == "fieldset"){
				var f;
				if(f = obj[x].id){
					obj[x].id = f.substr(0,f.lastIndexOf("_"))+"_"+length;
				}
			}else{
				var nodename = obj[x].nodeName.toLowerCase();
				var nodetype = obj[x].type.toLowerCase();
				var thename = obj[x].name;
				if(thename.length){
					obj[x].name = obj[x].name.substr(0,obj[x].name.lastIndexOf("_"))+"_"+length;
					obj[x].id = obj[x].id.substr(0,obj[x].id.lastIndexOf("_"))+"_"+length;
				}
			}
		}
	}
	hld.appendChild(newFset);
}
function removeFset(btn){
	var h = btn.parentNode.parentNode.parentNode;
	var lengths = h.getElementsByTagName("input");
	var length = lengths[0].value;
	alert(length);
	if(length > 1){
		if(confirm('Da li ste sigurni da želite da izbrišete podatke?')){
			h.removeChild(btn.parentNode.parentNode)
			lengths[0].value = --length;
		}
	}
}
function incrementNames(obj){
	for(x=0; x<obj.length; x++){
		if(obj[x].nodeName.toLowerCase() == "label"){
			var f = obj[x].getAttribute("for");
			var last = parseInt(f.substr(f.lastIndexOf("_")+1));
			last = last?last:1;
			var indx = last+1;
			obj[x].setAttribute("for",f.substr(0,f.lastIndexOf("_"))+"_"+indx);
		}else if(obj[x].nodeName.toLowerCase() == "fieldset"){
			var f = obj[x].id;
			var last = parseInt(f.substr(f.lastIndexOf("_")+1));
			alert("obj.id: "+obj[x].getAttribute("id"));
			last = last?last:1;
			var indx = last+1;
			obj[x].id = f.substr(0,f.lastIndexOf("_"))+"_"+indx;
		}else{
			var nodename = obj[x].nodeName.toLowerCase();
			var nodetype = obj[x].type.toLowerCase();
			var thename = obj[x].name;
			if(thename.length){
				var last = parseInt(thename.substr(thename.lastIndexOf("_")+1));
				last = last?last:1;
				var indx = last+1;
				obj[x].name = obj[x].name.substr(0,obj[x].name.lastIndexOf("_"))+"_"+indx;
				obj[x].id = obj[x].id.substr(0,obj[x].id.lastIndexOf("_"))+"_"+indx;
			}
		}
	}
}
function importFromBasics(btn,state){
	var i;
	var h;
	
	switch(state){
		case 1:
			h = btn.parentNode.parentNode;
			// email - web
			var email = document.getElementById("komitent_email").value;
			var web = document.getElementById("komitent_web").value;
			// brand
			var brands = document.getElementById("komitent_brands");
			var uls = h.getElementsByTagName("ul");
			for(var i=0; i<uls.length; i++){
				if(uls[i].id.match(/^komdel_brands_/)){
					uls[i].innerHTML = brands.innerHTML;
				}
			}
			
			// profili
			var profil_srp = document.getElementById("komitent_profil_srp").value;
			var profil_eng = document.getElementById("komitent_profil_en").value;
			
			var txts = h.getElementsByTagName("textarea");
			for(i=0; i<txts.length; i++){
				if(txts[i].name.match(/^komdel_profil_srp/)){
					txts[i].value = profil_srp;
				}else if(txts[i].name.match(/^komdel_profil_eng_/)){
					txts[i].value = profil_eng;
				}else if(txts[i].name.match(/^komdel_email_/)){
					txts[i].value = email;
				}else if(txts[i].name.match(/^komdel_web_/)){
					txts[i].value = web;
				}
			}
			
			break;
		case 2:
			h = btn.parentNode.parentNode;
			// adresa
			var region = document.getElementById("komitent_region");
			var opstina = document.getElementById("komitent_opstina");
			var mesto = document.getElementById("komitent_mesto");
			var lok_opstina = document.getElementById("komitent_lok_opstina");
			var naselje = document.getElementById("komitent_naselje");
			var ulica = document.getElementById("komitent_ulica").value;
			var ulica_id = document.getElementById("komitent_ulica_id").value;
			var broj = document.getElementById("komitent_ul_broj").value;
			// tel - fax
			var tel = document.getElementById("komitent_tel").value;
			var fax = document.getElementById("komitent_faks").value;
			// radno vreme
			var rvreme_dan = document.getElementById("komitent_rvreme_dan").value;
			var rvreme_sub = document.getElementById("komitent_rvreme_subota").value;
			var rvreme_ned = document.getElementById("komitent_rvreme_nedelja").value;
			
			var txts = h.getElementsByTagName("textarea");
			var inpts = h.getElementsByTagName("input");
			var sels = h.getElementsByTagName("select");
			for(i=0; i<txts.length; i++){
				if(txts[i].name.match(/^komdel_tel_/)){
					txts[i].value = tel;
				}else if(txts[i].name.match(/^komdel_faks_/)){
					txts[i].value = fax;
				}
			}
			for(i=0; i<inpts.length; i++){
				if(inpts[i].name.match(/^komdel_ulica_id_/)){
					inpts[i].value = ulica_id;
				}else if(inpts[i].name.match(/^komdel_ulica_\d/)){
					inpts[i].value = ulica;
				}else if(inpts[i].name.match(/^komdel_broj/)){
					inpts[i].value = broj;
				}else if(inpts[i].name.match(/^komdel_rvreme_dan_/)){
					inpts[i].value = rvreme_dan;
				}else if(inpts[i].name.match(/^komdel_rvreme_subota_/)){
					inpts[i].value = rvreme_sub;
				}else if(inpts[i].name.match(/^komdel_rvreme_nedelja_/)){
					inpts[i].value = rvreme_ned;
				}
			}
			for(i=0; i<sels.length; i++){
				if(sels[i].name.match(/^komdel_region_/)){
					sels[i].innerHTML = region.innerHTML;
					sels[i].options[region.selectedIndex].selected = true;
				}else if(sels[i].name.match(/^komdel_opstina_/)){
					sels[i].innerHTML = opstina.innerHTML;
					sels[i].options[opstina.selectedIndex].selected = true;
				}else if(sels[i].name.match(/^komdel_mesto_/)){
					sels[i].innerHTML = mesto.innerHTML;
					sels[i].options[mesto.selectedIndex].selected = true;
				}else if(sels[i].name.match(/^komdel_lok_opstina_/)){
					sels[i].innerHTML = lok_opstina.innerHTML;
					sels[i].options[lok_opstina.selectedIndex].selected = true;
				}else if(sels[i].name.match(/^komdel_naselje_/)){
					sels[i].innerHTML = naselje.innerHTML;
					sels[i].options[naselje.selectedIndex].selected = true;
				}
			}
			
			break;
	}
	alert("Kopiranje izvršeno.");
}
function togglePodaciDelatnosti(what,obj){
	if(document.error.length){
		alert("Prvo ispravite greske!")
		return;
	}
	
	var form = obj.parentNode.parentNode.parentNode.parentNode;
	var h = obj.parentNode.parentNode.parentNode.getElementsByTagName("h2")[0];
	if(h.innerHTML.toLowerCase() == "novi komitent"){
		
		var els = el = Array();
		var i;
		switch(what){
			case 0:	// na podaci
				break;
			case 1: // na delatnosti
				var pod = document.getElementById("podaci");
				var inputs	= pod.getElementsByTagName("input");
				var txts	= pod.getElementsByTagName("textarea");
				var sels	= pod.getElementsByTagName("select");
				
				for(i=0; i<inputs.length; i++){
					els.push(inputs[i]);
				}
				for(i=0; i<txts.length; i++){
					els.push(txts[i]);
				}
				for(i=0; i<sels.length; i++){
					els.push(sels[i]);
				}
				break;
		}
		
		var tst = testAllElements(els);
		window.status = "test:"+tst.value;
		if(tst.value==false){
			alert("Prvo ispravite greške!");
			tst.element.focus();
			if(tst.element.select){
				tst.element.select();
			}
			
			tst.element.style.background = "#f00";
			tst.element.style.color = "#fff";
			tst.element.onblur = function(){
				resetElementErrorStyle(this.id);
				this.onblur = function(){};
			}
			setTimeout("resetElementErrorStyle('"+tst.element.id+"')",2000);
			return;
		}
	}
	
	var lis = obj.parentNode.parentNode.getElementsByTagName("li");
	for(var i=0; i<lis.length; i++){
		if(lis[i] == obj.parentNode){
			lis[i].className = "current";
		}else{
			lis[i].className = "";
		}
	}
	if(what){	// delatnosti
		document.getElementById('podaci').style.display='none';
		document.getElementById('delatnosti').style.display='block';
	}else{		// podaci
		document.getElementById('podaci').style.display='block';
		document.getElementById('delatnosti').style.display='none';
	}
}

function resetElementErrorStyle(id){
	var obj = document.getElementById(id);
	obj.style.background=null;
	obj.style.color=null;
	if(obj.onblur){
		obj.onblur = null;
	}
}

/*******************************************************************************************/
/*******************************			KLASE		************************************/
/*******************************************************************************************/
function basic_data(){
	var tmp=null, tobj=null;
	this.required = Array("naziv","region","opstina","mesto","ulica_id","broj","telefon");
	this.reqErrMsg = Array();
	this.reqErrMsg["naziv"] = "Naziv";
	this.reqErrMsg["region"] = "Sedište - Region"
	this.reqErrMsg["opstina"] = "Sedište - Opština"
	this.reqErrMsg["mesto"] = "Sedište - Mesto"
	this.reqErrMsg["ulica_id"] = "Sedište - Ulica"
	this.reqErrMsg["broj"] = "Sedište - broj"
	this.reqErrMsg["telefon"] = "telefon"
	
	this.id = trim(document.getElementById("komitent_id").innerHTML);
	this.naziv		= trim(document.getElementById("komitent_ime").value);
	this.naziv_indx	= trim(document.getElementById("komitent_ime1").value);
	
	this.pib		= document.getElementById("komitent_pib").value;
	this.matbr		= document.getElementById("komitent_matbr").value;
	this.tip		= document.getElementById("komitent_tip").value;
	this.struktura	= document.getElementById("komitent_struktura").value;
	this.datum_osn	= document.getElementById("komitent_dat_osn").value;
	this.view_status= document.getElementById("komitent_view").value;
	this.direktor	= document.getElementById("komitent_direktor").value;
	this.direktor_tel	= document.getElementById("komitent_direktor_kontakt").value;
	this.predstavnik= document.getElementById("komitent_predstavnik").value;
	this.predstavnik_tel= document.getElementById("komitent_predstavnik_kontakt").value;
	this.vlasnik	= document.getElementById("komitent_vlasnik").value;
	this.vlasnik_jmbg= document.getElementById("komitent_vlasnik_jmbg").value;
	
	this.adresa_id	= document.getElementById("komitent_adresa_id").value;
	this.region		= document.getElementById("komitent_region").value;
	this.opstina	= document.getElementById("komitent_opstina").value;
	this.mesto		= document.getElementById("komitent_mesto").value;
	this.lokopstina	= document.getElementById("komitent_lok_opstina").value;
	this.naselje	= document.getElementById("komitent_naselje").value;
	this.ulica		= document.getElementById("komitent_ulica").value;
	this.ulica_id	= document.getElementById("komitent_ulica_id").value;
	this.broj		= document.getElementById("komitent_ul_broj").value;
	
	this.toll		= document.getElementById("komitent_toll").value;
	this.zasluga	= document.getElementById("komitent_zasluga").value;
	this.poreklo	= document.getElementById("komitent_poreklo").value;
	
	this.profilsrp 	= document.getElementById("komitent_profil_srp").value;
	//this.profilsrp	= this.profilsrp.replace(/\n/g,"<br />");
	this.profileng 	= document.getElementById("komitent_profil_en").value;
	//this.profileng	= this.profileng.replace(/\n/g,"<br />");
	this.napomena	= document.getElementById("komitent_napomena").value;
	//this.napomena	= this.napomena.replace(/\n/g,"<br />");
	
	var np = document.getElementById("nacini_placanja");
	this.nacini_placanja = Array();
	var nps = np.getElementsByTagName("input");
	for(var i=0; i<nps.length; i++){
		if(nps[i].type=="checkbox" && nps[i].checked){
			this.nacini_placanja.push(nps[i].value);
		}
	}
	
	this.racuni		= Array();
	var racuni = document.getElementById("racuni");
	var items = racuni.getElementsByTagName("div");
	for(var i=0; i<items.length; i++){
		var row = new Object();
		var tmp	= items[i].getElementsByTagName("input");
		if(tmp[0]){
			row.zr	= tmp[0].value;
			var tmp	= items[i].getElementsByTagName("select");
			row.banka = tmp[0].value
			this.racuni.push(row);
		}
	}
	this.telefon = Array();							// TELEFONI
	tobj = document.getElementById("komitent_tel");
	tmp = tobj.value.split("\n");
	for(x in tmp){
		this.telefon.push(tmp[x]);
	}
	this.faks = Array();							// FAKSOVI
	tobj = document.getElementById("komitent_faks");
	tmp = tobj.value.split("\n");
	for(x in tmp){
		this.faks.push(tmp[x]);
	}
	this.email = Array();							// EMAILS
	tobj = document.getElementById("komitent_email");
	tmp = tobj.value.split("\n");
	for(x in tmp){
		this.email.push(tmp[x]);
	}
	this.web = Array();								// WEBS
	tobj = document.getElementById("komitent_web");
	tmp = tobj.value.split("\n");
	for(x in tmp){
		this.web.push(tmp[x]);
	}
	this.brands = Array();							// BRANDS
	tobj = document.getElementById("komitent_brands");
	tobj = tobj.getElementsByTagName("li");
	if(tobj.length){
		for(i=0; i<tobj.length; i++){
			this.brands.push(tobj[i].id);
		}
	}/*
	this.brands = Array();							// BRANDS
	tobj = document.getElementById("komitent_brands");
	tmp = tobj.value.split("\n");
	for(x in tmp){
		this.brands.push(tmp[x]);
	}*/
	
	//kampanje
	this.kampanje = Array();
	var obj_kampanje_select = document.getElementById("komitent_kampanja");
	if(obj_kampanje_select){
		for(i=0; i<obj_kampanje_select.options.length; i++){
			var o = obj_kampanje_select.options[i].value;
			this.kampanje.push(o);
		}
	}

	var rd = document.getElementById("komitent_rvreme_dan").value;				//***************//
	var subota = document.getElementById("komitent_rvreme_subota").value;		//  RADNO VREME  //
	var nedelja = document.getElementById("komitent_rvreme_nedelja").value;		//***************//
	this.radnovreme = Array();
	this.radnovreme["rd"] = rd;
	this.radnovreme["sub"] = subota;
	this.radnovreme["ned"] = nedelja;
	/*this.radnovreme.push(rd);
	this.radnovreme.push(subota);
	this.radnovreme.push(nedelja);*/
	
	/************************************************/
	/*					DELATNOSTI					*/
	/************************************************/
	var delHldr = document.getElementById("delatnosti");
	if(!delHldr){
		alert("Fatal error!");
		return;
	}
	var delsUL = document.getElementById("komitent_delatnosti");
	var dels = delsUL.getElementsByTagName("li");
	
	// delatnosti
	this.delatnosti_length = dels.length;
	this.delatnosti = Array();
	for(var i=0; i<dels.length; i++){
		// delatnost id
		delid = dels[i].id;
		//alert(delid);
		if(delid != "del0"){		// ako nije del0 za bug fix firefox-a
			var tmpDel = new delatnost(delid);
			
			// delname
			var span = dels[i].getElementsByTagName("span");
			span = span[1];
			tmpDel.naziv = span.innerHTML.substr(span.innerHTML.indexOf("&nbsp;")+6);
			
			//glavna
			var inptgl = dels[i].getElementsByTagName("input");
			inptgl = inptgl[0];
			tmpDel.glavna = inptgl.checked?1:0;
			
			// profil srpski
			var obj_profil_srp = document.getElementById("komdel_profil_srp_"+delid);
			tmpDel.profilsrp = trim(obj_profil_srp.value);
			
			// profil engleski
			var obj_profil_eng = document.getElementById("komdel_profil_eng_"+delid);
			tmpDel.profileng = trim(obj_profil_eng.value);
			
			// email
			var obj_email = document.getElementById("komdel_email_"+delid);
			tmpDel.email = trim(obj_email.value).split(/\n/);
			// web
			var obj_web = document.getElementById("komdel_web_"+delid);
			tmpDel.web = trim(obj_web.value).split(/\n/);
			// keywords srb
			var obj_kwords_srb = document.getElementById("komdel_keywords_srb_"+delid);
			tmpDel.kwords_srb = trim(obj_kwords_srb.value).split(/\n/);
			// keywords eng
			var obj_kwords_eng = document.getElementById("komdel_keywords_eng_"+delid);
			tmpDel.kwords_eng = trim(obj_kwords_eng.value).split(/\n/);
			// brands
			var obj_brands = document.getElementById("komdel_brands_"+delid);
			obj_brands = obj_brands.getElementsByTagName("li");
			if(obj_brands.length>0){
				tmpDel.brands = Array();
				for(j=0; j<obj_brands.length; j++){
					tmpDel.brands.push(obj_brands[j].id);
				}
			}
			//var obj_brands = document.getElementById("komdel_brands_"+delid);
			//tmpDel.brands = trim(obj_brands.value).split(/\n/);
			
			// podaci
			// delatnosti holders (blockquotes)
			var dels_bq = document.getElementById("komitent_delatnosti_data").getElementsByTagName("blockquote");
			var tmp = Array();
						for(var j=0; j<dels_bq.length; j++){
				if(dels_bq[j].id != "template" && dels_bq[j].id == delid){
					tmp[delid] = dels_bq[j];
				}
			}
			dels_bq = tmp;
			
			
			
			var fs = dels_bq[delid].getElementsByTagName("fieldset");
			for(j=0; j<fs.length; j++){
				var data_holder = fs[j];
				
				var komdel = data_holder.getAttribute("komdel");
				var tmpDelData = new delatnost_data(komdel);
				
				var inputs = data_holder.getElementsByTagName("input");
				var textareas = data_holder.getElementsByTagName("textarea");
				var selects = data_holder.getElementsByTagName("select");
				var uls = data_holder.getElementsByTagName("ul");
				
				var obj;
				var buff;
				var m;
				// UL
				for(k=0; k<uls.length; k++){
					obj = uls[k];
					var lis = obj.getElementsByTagName("li");
					if(obj.id.match(/^komdel_cards_/)){			// kartice
						for(m=0; m<lis.length; m++){
							tmpDelData.cards.push(lis[m].getAttribute("value"));
						}
					}
				}
				// INPUT
				for(k=0; k<inputs.length; k++){
					obj = inputs[k];
					obj.value = trim(obj.value);
					if(obj.name.match(/^komdel_adresa_\d/)){			// adresa_id
						tmpDelData.adresa_id = obj.value;
					}else if(obj.name.match(/^komdel_ulica_\d/)){		// ulica
						tmpDelData.ulica = obj.value;
					}else if(obj.name.match(/^komdel_ulica_id_/)){		// ulica_id
						tmpDelData.ulica_id = obj.value;
					}else if(obj.name.match(/^komdel_broj/)){			// broj
						tmpDelData.broj = obj.value;
					}else if(obj.name.match(/^komdel_rvreme_dan_/)){	// rad.vreme radni dan
						tmpDelData.rvreme["rd"] = obj.value;
					}else if(obj.name.match(/^komdel_rvreme_subota_/)){	// rad.vreme subota
						tmpDelData.rvreme["sub"] = obj.value;
					}else if(obj.name.match(/^komdel_rvreme_nedelja_/)){// rad.vreme nedelja
						tmpDelData.rvreme["ned"] = obj.value;
					}
				}
				
				// TEXTAREA
				for(k=0; k<textareas.length; k++){
					obj = textareas[k];
					obj.value = trim(obj.value);
					var items = obj.value.split(/\n/);
					if(items.length>0 && items[0].length>0){
						if(obj.name.match(/^komdel_tel_/)){				// telefon
							tmpDelData.telefon = items;
						}else if(obj.name.match(/^komdel_faks/)){		// faks
							tmpDelData.faks = items;
						}
					}
				}
				
				// SELECT
				for(k=0; k<selects.length; k++){
					obj = selects[k];
					if(obj.name.match(/^komdel_region_/)){				// region
						tmpDelData.region = obj.value;
					}else if(obj.name.match(/^komdel_opstina_/)){		// opstina
						tmpDelData.opstina = obj.value;
					}else if(obj.name.match(/^komdel_mesto_/)){			// mesto
						tmpDelData.mesto = obj.value;
					}else if(obj.name.match(/^komdel_lok_opstina_/)){	// lok.opstina
						tmpDelData.lokopstina = obj.value;
					}else if(obj.name.match(/^komdel_naselje_/)){		// naselje
						tmpDelData.naselje = obj.value;
					}
				}
				
				// append podaci array
				tmpDel.podaci.push(tmpDelData);
			}
			// append delatnosti array
			this.delatnosti.push(tmpDel);
		}
	}
	
	this.display 		= displayData;
	this.check			= checkData;
	this.createReport 	= createReport;
	this.send			= sendData;
	this.getStructure	= getStructure;
}
function getStructure(){
	var partner = this;
	var out="";
	for(x in partner){
		if(typeof(partner[x]) != "function"){
			out+=x+": ";
			if(typeof(partner[x]) != "object"){
				out+=partner[x]+"\n";
			}else{
				var obj = partner[x];
				out+="\n"
				for(y in obj){
					out += "     "+y+": ";
					if(typeof(obj[y]) != "object"){
						out+=obj[y]+"\n";
					}else{
						out+="\n"
						var objobj = obj[y];
						for(z in objobj){
							out += "          "+y+": "+objobj[z]+"\n";
						}
					}
				}
			}
		}
	}
	var alrt = document.createElement("div");
	alrt.style.position	= "absolute";
	alrt.style.width	= "500px";
	alrt.style.left		= "30px";
	alrt.style.top		= "30px";
	alrt.style.textAlign= "left";
	alrt.style.border	= "1px solid #000";
	alrt.style.padding	= "10px";
	alrt.style.background="#fff";
	out = out.replace(/ /g,"&nbsp;");
	out = out.replace(/\n/g,"<br />");
	alrt.innerHTML = out;
	document.body.appendChild(alrt);
}
function delatnost(id){
	this.required	= Array();
	this.reqErrMsg	= Array();
	this.id			= id;
	this.naziv		= "";
	this.glavna		= 0;
	this.profilsrp	= "";
	this.profileng	= "";
	this.email		= Array();
	this.web		= Array();
	this.kwords_srb	= Array();
	this.kwords_eng	= Array();
	this.brands		= Array();
	this.podaci		= Array();

}
function delatnost_data(komdel_id){
	//this.required	= Array("region","opstina","mesto","ulica_id","broj","telefon");
	this.required	= Array("telefon");
	this.reqErrMsg	= Array();
	this.reqErrMsg["region"] = "";
	this.reqErrMsg["opstina"] = "";
	this.reqErrMsg["mesto"] = "";
	this.reqErrMsg["ulica_id"] = "";
	this.reqErrMsg["broj"] = "";
	
	this.komdel		= komdel_id;
	this.adresa_id  = "";
	this.region		= "";
	this.opstina	= "";
	this.mesto		= "";
	this.lokopstina	= "";
	this.naselje	= "";
	this.ulica_id	= "";
	this.ulica		= "";
	this.broj		= "";
	this.telefon	= Array();
	this.faks		= Array();
	this.rvreme		= Array();
	this.cards		= Array();
}
function createReport(){
	var newWin = window.open("","_blank","statusbar=0, menubar=0");
	var html = "<h1>"+this.naziv+"</h1><p>";
	html += "<strong>PIB: </strong>"+this.pib+"<br />";
	html += "<strong>Matični broj: </strong>"+this.matbr+"<br />";
	html += "<strong>Tip: </strong>"+this.tip+"<br />";
	html += "<strong>Struktura: </strong>"+this.struktura+"<br />";
	html += "<strong>Datum osnivanja: </strong>"+this.datum_osn+"<br />";
	html += "<strong>Želi da se vidi u: </strong>"+this.view_status+"<br />";
	html += "<strong>Direktor: </strong>"+this.direktor+" ("+this.direktor_tel+")<br />";
	html += "<strong>Predstavnik: </strong>"+this.predstavnik+" ("+this.predstavnik_tel+")<br />";
	html += "<strong>Vlasnik: </strong>"+this.vlasnik+" ("+this.vlasnik_jmbg+")<br />";
	html += "<strong>Region: </strong>"+this.region+"<br />";
	html += "</p>";
	newWin.document.write(html);
}
function displayData(){
	d = this;
	
	out="";
	for(x in d){
		if(typeof(d[x]) != "function"){
			if(typeof(d[x]) != "object"){
				out += x+": "+d[x];
			}else{
				out += "\n"+x+": ";
			}
			out += "\n";
		}
		if(typeof(d[x]) == "object"){
			//out+=d[x].length+"["+typeof(d[x])+"]\n";
			//out += "---------------------\n";
			for(var i=0; i<d[x].length; i++){
				var it = d[x][i];
				if(typeof(it)=="object"){
					out += "\t+---"+(i+1)+"\n";
					for(y in it){
						if(typeof(it[y]) != "object"){
							out += "\t\t+---"+y+": "+it[y]+"\n";
						}else{
							out += "\t\t+---"+y+":\n";
						}
						if(typeof(it[y]) == "object"){
							for(var j=0; j<it[y].length; j++){
								var it1 = it[y][j];
								//out += "\t"+(j+1)+"\n";
								if(typeof(it1)=="object"){
									for(z in it1){
										out += "\t\t\t+---"+z+": ";
										if(typeof(it1[z]) != "object")
											out += it1[z];
										else{
											for(w in it1[z]){
												var it2 = it1[z][w];
												out += "\n\t\t\t\t+---"+w+": ";
												if(typeof(it2) != "object"){
													out += it2;
												}else{
													for(q in it2){
														var it3 = it2[q];
														if(typeof(it3) != "object"){
															out += "\n\t\t\t\t+---"+it3
														}else{
															for(a in it3){
																out += "\n\t\t\t\t\t+---"+it3[a]
															}
														}
													}
												}
												out += "\n";
											}
										}
										out += "\n";
									}
								}else{
									out += "\t\t\t+---"+it1+"\n";
								}
							}
						}
					}
				}else{
					out += "\t+---"+it;
				}
				out += "\n";
			}
		}
	}
	var dbgTXT = document.getElementById("debugTXT");
	if(!dbgTXT){
		dbg = document.createElement("div");
		dbg.style.position = "absolute";
		dbg.style.top = "50px";
		dbg.style.left = "650px";
		dbg.style.width = "600px";
		dbg.style.height = "600px";
		dbg.style.textAlign = "right";
		dbg.id = "debug";
		
		var dbgTXT = document.createElement("textarea");
		dbgTXT.id = "debugTXT";
		dbgTXT.style.width = "600px";
		dbgTXT.style.height = "550px";
		
		var dbgClose = document.createElement("a");
		dbgClose.href="javascript:void(0)";
		dbgClose.style.position = "absolute";
		dbgClose.style.margin = "13px 0px 0px -48px";
		dbgClose.onclick = function(){
			this.parentNode.style.display='none';
		}
		dbgClose.innerHTML = '<img border="0" src="gui/close.gif" alt="" />';
		
		
		dbg.appendChild(dbgTXT);
		dbg.appendChild(dbgClose);
		document.body.appendChild(dbg);
	}
	dbg.style.display = "block";
	dbgTXT.value = out;	
}
function checkData(){
	var chk = checkIt(this);
	var docErr =  (document.error.length>0)?document.error:false;
	if((chk === true) && (docErr===false)){
		if(!this.delatnosti.length){
			imaGresaka("Nema ni jedne delatnosti!");
			//alert("Niste dodali ni jednu delatnost!");
			return false;
		}else{
			for(var i=0; i<this.delatnosti.length; i++){
				if(!this.delatnosti[i].podaci.length){
					imaGresaka("Nema podataka za delatnost "+this.delatnosti[i].naziv);
					return false;
				}else{
					for(var j=0; j<this.delatnosti[i].podaci.length; j++){
						var data = this.delatnosti[i].podaci[j];
						if(data.region<1){
							imaGresaka("Niste odabrali region u delatnosti "+this.delatnosti[i].naziv);
							return false;
						}else if(data.opstina<1){
							imaGresaka("Niste odabrali opštinu u delatnosti "+this.delatnosti[i].naziv);
							return false;
						}else if(data.mesto<1){
							imaGresaka("Niste odabrali mesto u delatnosti "+this.delatnosti[i].naziv);
							return false;
						}else if(data.ulica_id<1){
							imaGresaka("Niste odabrali ulicu u delatnosti "+this.delatnosti[i].naziv);
							return false;
						}else if(data.broj<1){
							imaGresaka("Niste uneli broj adrese u delatnosti "+this.delatnosti[i].naziv);
							return false;
						}else if(data.telefon.length<1){
							imaGresaka("Niste uneli broj telefona u delatnosti "+this.delatnosti[i].naziv);
							return false;
						}
						/*var out = ""
						for(x in data){
							out += x+": "+data[x]+"\n"
						}*/
						//alert(data.telefon.length)
					}
				}
			}
			return true;
		}
	}else{
		var msg="";
		if(docErr !== false){
			msg = "Označena polja nisu validna!";
			/*if(document.getElementById("ErrorDiv")){
				do{
					var ed = document.getElementById("ErrorDiv");
					ed.parentNode.removeChild(ed);
				}while(document.getElementById("ErrorDiv"));
			}*/
			for(var i=0; i<docErr.length; i++){
				var err = docErr[i];
				err.setMarker();
			}
		}else{
			msg = "Obavezno polje \""+chk+"\" je prazno!";
		}
		imaGresaka(msg,docErr);
		return false;
	}
}
function testDocError(){
	var de = document.error;
	var out = ""
	for(var i=0; i<de.length; i++){
		for(x in de[i]){
			out += x+": "+de[i][x]+"\n";
		}
		out += "\n";
	}
	alert(out)
}
function checkIt(o){
	var test=null;
	for(x in o){
		var obj = o[x];
		var t = typeof(obj);
		t = t.toLowerCase();
		test=isRequired(x,o.required);
		//alert(x+"["+t+"]\nisreq: "+test+"\nlen:"+obj.length);
		if(test && t != "function"){
			if(t=="object"){
				if(obj.length<1 || (obj.length==1 && trim(obj[0])<1)){
					return o.reqErrMsg[x];
				}
			}else{
				obj = trim(obj);
				if(!obj.length || obj<0){
					return o.reqErrMsg[x];
				}
			}
		}
	}
	return true;
}
function isRequired(needle, haystack){
	ret = false;
	if(!haystack){
		return ret;
	}
	for(var i=0; i<haystack.length; i++){
		if(haystack[i] == needle){
			ret = true;
			break;
		}
	}
	return ret;
}
function imaGresaka(poruka,de){
	var es = document.getElementById("ErrorStatus");
	if(de){
		var id=Array();
		// htmlObject
		if(de.nodeName){
			id.push(de.id);
		// array errorObject
		}else{
			for(var i=0; i<de.length; i++){
				id.push(de[i].element.id);
			}
		}
		if(es)
		es.innerHTML = '<a href="javascript:void(0)" id="ErrorLink" onclick="markErrorObject(\''+id+'\')">'+poruka+'</a>';
	}else{
		if(es){
			es.innerHTML = poruka;
		}else{
			if(poruka)
			alert(poruka)
		}
	}
}
function markErrorObject(id){
	ids = id.split(/,/);
	for(var i=0; i<ids.length; i++){
		var obj = document.getElementById(ids[i]);
		var eo = new errorObject(1,obj,obj.name,obj.id);
		eo.setMarker();
	}
}
function sendData(){
	out = "";
	for(x in this){
		var t1 = typeof(this[x]);
		t1 = t1.toLowerCase();
		if(t1 != "function"){
			// LEVEL 1
			if(t1 == "object"){
				var obj2 = this[x];
				var line2 = "";
				for(y in obj2){
					var t2 = typeof(obj2[y]);
					t2 = t2.toLowerCase();
					// LEVEL 2
					if(t2 == "object"){
						var obj3 = obj2[y];
						var line3 = "";
						for(z in obj3){
							var t3 = typeof(obj3[z]);
							t3 = t3.toLowerCase();
							// LEVEL 3
							if(t3 == "object"){
								var obj4 = obj3[z];
								var line4 = "";
								for(w in obj4){
									var t5 = typeof(obj4[w]);
									t5 = t5.toLowerCase();
									// LEVEL 4
									if(t5 == "object"){
										var obj5 = obj4[w];
										var line5 = "";
										for(q in obj5){
											var t6 = typeof(obj5[q]);
											t6 = t6.toLowerCase();
											// LEVEL 5
											if(t6 == "object"){
												var obj6 = obj5[q];
												var line6 = "";
												for(p in obj6){
													line6 += p+"{======}"+escape(obj6[p])+"{!!!!!!}";
												}
												line5 += q+"{=====}"+line6.substr(0,line6.length-8)+"{!!!!!}";
											}else{
												line5 += q+"{=====}"+escape(obj5[q])+"{!!!!!}";
											}
										}
										line4 += w+"{====}"+line5.substr(0,line5.length-7)+"{!!!!}";
									}else{
										line4 += w+"{====}"+escape(obj4[w])+"{!!!!}";
									}
								}
								line3 += z+"{===}"+line4.substr(0,line4.length-6)+"{!!!}";
							}else{
								line3 += z+"{===}"+escape(obj3[z])+"{!!!}";
							}
						}
						line2 += y+"{==}"+line3.substr(0,line3.length-5)+"{!!}";
					}else{
						line2 += y+"{==}"+escape(obj2[y])+"{!!}";
					}
				}
				out += x+"{=}"+line2.substr(0,line2.length-4)+"{!}";
			}else{
				out += x+"{=}"+escape(this[x])+"{!}"
			}
		}
	}
	out = out.substr(0,out.length-3);
	out = out.replace(/\+/g,"%2b");
	/*var dbg = document.createElement("div");
	dbg.style.position = "absolute";
	dbg.style.width = "800px";
	dbg.style.height = "600px";
	dbg.style.top = "5px";
	dbg.style.left = "5px";
	dbg.style.background = "#FFFFFF";
	dbg.style.border = "1px solid #000";
	dbg.style.textAlign = "left";
	dbg.style.overflow = "auto";
	dbg.innerHTML = out;
	document.body.appendChild(dbg);*/
	var action = "a=save&data="+out;
	//LoadRSDoc("komitenti.php","setContent",action,"",1);
	LoadRSDoc("komitenti.php","afterSave("+this.id+")",action,"",1);
}
function afterSave(content,id){
	var tmp = content.split("{!}",2);
	var err, msg;
	if(tmp.length==2){
		err = parseInt(tmp[0]);
		msg = tmp[1];
	}else{
		err = 0;
		msg = content;
	}
	alert(msg);
	if(!err){
		if(id){
			LoadRSDoc("komitenti.php?a=displayOne&id="+id,"setContent");
		}else{
			LoadRSDoc("komitenti.php?a=new","setContent");
		}
	}
	/*var newWin = window.open("about:blank","_savekom","width=800, height=600, scrollbars=1");
	var out = "<html>";
	out += "<head>";
	out += "<title>Save komitent debug window</title>";
	out += "</head>";
	out += "<body>"+content+"</body>";
	out += "</html>";
	newWin.document.write(out);
	newWin.focus();*/
}
function dodajBanku(btn,indx){
	o = duplicateObject1(btn.parentNode,btn.parentNode.parentNode);
	if(indx==1){
		var tmp = o.innerHTML;
		var rgxStart = /<!--[\s\n]*<input/;
		var rgxEnd = /\/>[\s\n]*-->/
		tmp = tmp.replace(rgxStart,"<input");
		tmp = tmp.replace(rgxEnd,"/>");
		o.innerHTML = tmp;
	}
}
function obrisiBanku(btnRem){
	btnRem.parentNode.parentNode.removeChild(btnRem.parentNode);
}
function testBtn(){
	var hldr = document.getElementById("komitent_delatnosti_data");
	var fs = hldr.getElementsByTagName("fieldset");
	var out="";
	for(var i=0; i<fs.length; i++){
		out += (i+1)+". "+fs[i].id+"\n";
	}
	alert(out);
}
function searchKomitent(forma,type,kword){
	kword = escape(kword);
	kword = kword.replace("+","%2b");
	LoadRSDoc('komitenti.php?a=search&t='+type+'&k='+kword,'ypAjaxForm');
}
function selectKomitent(objname,objid,funcs){
	var ask = trim(document.getElementById(objname).value);
	var kword = prompt("Naziv komitenta:",ask);
	if(kword){
		if(kword.match(/\d+/)){
			var type="sifra";
		}else{
			var type="ime";
		}
		LoadRSDoc("komitenti.php?a=search&t="+type+"&k="+escape(kword)+"&l=1&objname="+objname+"&objid="+objid+"&addfuncs="+funcs,"ypAjaxForm");
	}
}
function setKomitent(kom,komid,li){
	var objid = document.getElementById(kom[0]);
	var objname = document.getElementById(kom[1]);
	
	objid.value = komid;
	var tmp = li.innerHTML.split(/\s*<strong(.*?)<\/strong>/g);
	var tmp = tmp[1];
	
	tmp = tmp.split(/<span.*?>|<\/span>/);
	var out="";
	for(var i=0; i<tmp.length; i++){
		out += tmp[i];
	}
	objname.value = out.substr(1);
}
function advanceKomitentSearch(forma,e){
	var els = forma.elements;
	var action = "a=search";
	if(els["kword"] && trim(els["kword"].value).length==0){
		return;
	}
	for(var i=0; i<els.length; i++){
		els[i].value = trim(els[i].value);
		action += "&"+els[i].name+"="
		if(els[i].type=="checkbox"){
			action += els[i].checked?1:0;
		}else{
			action += escape(els[i].value);
		}
		
		if(els[i].name == "lok_opstina_komitent"){
			var lopstine = "";
			var naselja  = "";
			if(els[i].value>0){
				if(els[i+1].value<0){
					for(var j=0; j<els[i+1].options.length; j++){
						if(parseInt(els[i+1].options[j].getAttribute("parent"))==els[i].value){
							naselja += ";"+els[i+1].options[j].value;
						}
					}
					action += "&naselja="+naselja.substr(1);
				}
			}else{
				for(var j=0; j<els[i].options.length; j++){
					if(parseInt(els[i].options[j].getAttribute("parent"))==els[i-1].value){
						lopstine += ";"+els[i].options[j].value;
					
						for(var k=0; k<els[i+1].options.length; k++){
							if(parseInt(els[i+1].options[k].getAttribute("parent"))==els[i].options[j].value){
								naselja += ";"+els[i+1].options[k].value;
							}
						}
					}
				}
			}
			action += "&naselja="+naselja.substr(1);
			action += "&lopstine="+lopstine.substr(1);
		}
	}
	if(e && e.ctrlKey){
		var func = "afterAdvSearch(1)";
		action += "&directDownload=1";
	}else{
		var func = "afterAdvSearch";
	}
	LoadRSDoc("komitenti.php",func,action,'',1);
}
function afterAdvSearch(response,dd){
	if(dd){
		ypAjaxForm(response);
	}else{
		document.getElementById("pretraga").innerHTML = response;
	}
}
function advSearchFilterDelatnosti(kat_obj){
	var katid = kat_obj.value;
	//var del_obj = document.getElementById("delatnost");
	var del_obj = kat_obj.nextSibling.nextSibling.nextSibling.nextSibling.nextSibling;
	
	var opts = del_obj.options
	var selected = false;
	for(var i=0; i<opts.length; i++){
		var delkat = opts[i].getAttribute("katid");
		if(katid != delkat && katid>0){
			opts[i].style.display="none";
		}else{
			if(!selected){
				opts[i].selected=true;
				selected=true;
			}
			opts[i].style.display="block";
		}
	}
}
function advSearchBezKampanjaAdd(){
	var ul = document.getElementById("BezKampanjaUL");
	if(ul){
		var lengthObj = document.getElementById("bez_kampanja_length");
		var lis = ul.getElementsByTagName("li");
		var nli = lis[lis.length-1].cloneNode(true);
		var sel = nli.getElementsByTagName("select")[0];
		var inc = getIncrement(sel.name);
		var newid = parseInt(inc["extension"])+1;
		sel.name = inc["base"]+"_"+newid;
		sel.id = sel.name;
		ul.appendChild(nli);
		lengthObj.value = newid;
	}
}
function advSearchBezKampanjaRemove(){
	var ul = document.getElementById("BezKampanjaUL");
	if(ul){
		var lengthObj = document.getElementById("bez_kampanja_length");
		var lis = ul.getElementsByTagName("li");
		if(lis.length>1){
			ul.removeChild(lis[lis.length-1]);
			lengthObj.value = parseInt(lengthObj.value)-1;
		}else{
			var sel = lis[0].getElementsByTagName("select")[0];
			sel.selectedIndex=0;
		}
	}
}
function makeKomercijalaTask(table){
	var tr = table.getElementsByTagName("tr");
	var num=0;
	var action="a=manager&mod=komercijalaTask";
	for(var i=0; i<tr.length; i++){
		if(tr[i].className=="selected"){
			var td = tr[i].getElementsByTagName("td");
			action += "&komid_"+num+"="+trim(td[1].innerHTML);
			action += "&komname_"+num+"="+escape(trim(td[2].innerHTML));
			action += "&komaddress_"+num+"="+escape(trim(td[3].innerHTML));
			action += "&komdels_"+num+"="+escape(trim(td[9].innerHTML));
			num++;
		}
	}
	action+="&length="+num;
	if(num>0){
		LoadRSDoc("rokovnik.php","afterMakeKomercijalaTask",action,'',1);
	}else{
		alert("Morate odabrati komitente!");
	}
}
function afterMakeKomercijalaTask(response){
	odstampaj(response);
}
function toggleWebInfo(obj,id,delid){
	var bq = obj.parentNode.parentNode.parentNode;
	var fsets = bq.getElementsByTagName("fieldset");
	var io = getElementsByClassName(obj.parentNode,"webInfo");
	if(io.length>0){
		var infoObj = obj.nextSibling.nextSibling;
		obj.parentNode.removeChild(infoObj);
	}else{
		var infoObj = document.createElement("div");
		infoObj.className = "webInfo";
		obj.parentNode.appendChild(infoObj);
		
		var komdel=null;
		for(var i=0; i<fsets.length; i++){
			if(fsets[i].getAttribute("www") == 1){
				komdel = fsets[i].getAttribute("komdel");
				break;
			}
		}
		if(komdel == null){
			komdel = fsets[0].getAttribute("komdel");
			//komdel += " by script";
		}
		
		infoObj.style.display = "block";
		LoadRSDoc('komitenti.php?a=wwwdata&komdel='+komdel,"simpleInject",infoObj);
	}
}
function saveWWWData(btn){
	var inpts = btn.parentNode.getElementsByTagName("input");
	var selects = btn.parentNode.getElementsByTagName("select");
	var textareas = btn.parentNode.getElementsByTagName("textarea");
	var objs = Array(inpts, selects, textareas);

	var action = "a=wwwdata&mod=save";
	for(x in objs){
		var obj = objs[x];
		for(var i=0; i<obj.length; i++){
			if(obj[i].name){
				action += "&"+obj[i].name+"="+escape(trim(obj[i].value));
			}
		}
	}
	LoadRSDoc("komitenti.php","afterSaveWWWdata('"+btn.parentNode.id+"')",action,"",1);
}
function afterSaveWWWdata(response,id){
	alert(response);
	var holder = document.getElementById(id).parentNode;
	if(holder){
		holder.parentNode.removeChild(holder);
	}
}
function importWWWData(btn,komid,delid){
	LoadRSDoc("komitenti.php?a=wwwdata&mod=import&komid="+komid+"&delid="+delid+"&objid="+btn.parentNode.id,"ypAjaxForm");
}
function updateImportWWWData(li,objid){
	closeYpAjaxForm();
	var dest_obj = document.getElementById(objid);
	if(dest_obj){
		var dest_inputs = dest_obj.getElementsByTagName("input");
		var dest_txts = dest_obj.getElementsByTagName("textarea");
		
		var tmp = Array();
		for(var i=0; i<dest_inputs.length; i++){
			if(dest_inputs[i].type == "text"){
				tmp.push(dest_inputs[i])
			}
		}
		dest_inputs = tmp;
		
		var fs = li.getElementsByTagName("fieldset");
		var logo = fs[0].innerHTML.split(/<span>(.*?)<\/span>/);
		var logo_name = logo[1];
		var logo_start = logo[3];
		var logo_end = logo[5];
		
		var baner = fs[1].innerHTML.split(/<span>(.*?)<\/span>/);
		var baner_name = baner[1];
		var baner_start = baner[3];
		var baner_end = baner[5];
		
		var host = fs[2].innerHTML.split(/<span>(.*?)<\/span>/);
		var test = host[3];
		var final = host[5];
		var host = host[1];
		
		for(i=0; i<dest_inputs.length; i++){
			var inname = dest_inputs[i].name;
			switch(inname){
				case "logo_start":
					dest_inputs[i].value = logo_start;
					break;
				case "logo_end":
					dest_inputs[i].value = logo_end;
					break;
				case "logo_path":
					dest_inputs[i].value = logo_name;
					break;
				case "baner_start":
					dest_inputs[i].value = baner_start;
					break;
				case "baner_end":
					dest_inputs[i].value = baner_end;
					break;
				case "baner_name":
					dest_inputs[i].value = baner_name;
					break;
				case "hosting":
					dest_inputs[i].value = host;
					break;
				case "test_adresa":
					dest_inputs[i].value = test;
					break;
				case "kon_adresa":
					dest_inputs[i].value = final;
					break;
			}
		}
		dest_txts[0].value = baner_name;
	}
}
function copyKomdelData(btn){
	copyDel(btn);
}
function pasteKomdelData(btn,komid){
	pasteDel(btn,komid);
}
function copyValues(source,dest){
	var Sinpt	= source.getElementsByTagName("input");
	var Stxt	= source.getElementsByTagName("input");
	var Ssel	= source.getElementsByTagName("select");
	
	var Dinpt	= dest.getElementsByTagName("input");
	var Dtxt	= dest.getElementsByTagName("input");
	var Dsel	= dest.getElementsByTagName("select");
	
	// inputs
	for(var i=0; i<Sinpt.length; i++){
		if(Sinpt.type == "text"){
			Dinpt[i].value = Sinpt[i].value;
		}
	}
	return dest;
}
function testUgovori(response){
	var icon = document.getElementById("ugovori_komitent");
	if(trim(response).length >0){
		icon.style.display = "block";
		icon.onclick = function(){
			//alert(response);
			ypAjaxForm(response);
		}
	}else{
		icon.style.display = "none";
	}
}
function listingBindAddFile(btn){
	var div = btn.parentNode;
	var cdiv = div.cloneNode(true);
	cdiv.id = parseInt(cdiv.id)+1;
	var inc = cdiv.id;
	var file = cdiv.getElementsByTagName("input");
	file = file[0];
	file.id = file.id.replace(/\d$/,"")+inc
	file.name = file.id;
	var lbl = cdiv.getElementsByTagName("label");
	lbl = lbl[0];
	lbl.innerHTML = inc+":";
	lbl.setAttribute("for",file.id);

	div.parentNode.insertBefore(cdiv,div.nextSibling);
	btn.parentNode.parentNode.num.value = inc;
	var inpts = div.getElementsByTagName("input");
	for(var i=0; i<inpts.length; i++){
		if(inpts[i].type=="button"){
			inpts[i].style.display="none";
		}
	}
}
function listingBindRemoveFile(btn){
	var num = btn.parentNode.parentNode.num;
	if(num.value > 1){
		var div = btn.parentNode;
		var divPrev = div.previousSibling
		var inpts = divPrev.getElementsByTagName("input");
		for(var i=0; i<inpts.length; i++){
			if(inpts[i].type=="button"){
				inpts[i].style.display="block";
			}
		}
	
		div.parentNode.removeChild(div);
		
		num.value = num.value-1;
	}
}
function genListingDelatnosti(forma){
	var els = forma.elements;
	var del = els["delatnost"].value;
	var kampanja=els["kampanja"].value;
	
	LoadRSDoc("komitenti.php?a=listing&delatnost="+del+"&kampanja="+kampanja,"setContent");
	
}
function showListingSettings(func,kampanja){
	if(!func){
		func="ypAjaxForm";
	}
	var action = 'komitenti.php?a=listing&mod=settings&func='+func;
	if(kampanja){
		action += "&kampanja="+kampanja
	}
	LoadRSDoc(action,func);
}
function newListingSetting(){
	var ld = document.getElementById("settingsData");
	var span = document.createElement("span");
	var label = document.createElement("label");
	var btn = document.createElement("input");
	var sel = document.getElementById("templateOpstine").cloneNode(true);
	
	var sels = ld.getElementsByTagName("select");
	var length = sels.length+1;
	
	label.setAttribute("for","opstina_"+length);
	label.innerHTML = length+":";
	span.appendChild(label);
	
	sel.name = "opstina_"+length;
	sel.id = "opstina_"+length;
	span.appendChild(sel);
	
	btn.type = "button";
	btn.value = "";
	btn.className = "del";
	btn.onclick = function(){
		deleteListingSetting(this);
	}
	span.appendChild(btn);
	
	ld.appendChild(span);
	sel.focus();
}
function deleteListingSetting(btn){
	if(confirm('Potvrdite brisanje?')){
		btn.parentNode.parentNode.removeChild(btn.parentNode);
		var span = document.getElementById("settingsData").getElementsByTagName("span");
		for(var i=0; i<span.length; i++){
			var sp = span[i];
			var lbl = sp.getElementsByTagName("label");
			lbl = lbl[0];
			lbl.innerHTML = i+1+":";
		}
	}
}
function afterSaveSettings(response,kampanja){
	closeYpAjaxForm();
	alert(response);
	//showListingSettings('ypAjaxForm',kampanja);
}
function exportListingTXT(table,delatnost){
	var delid = document.getElementById("delatnost_id").value;
	var delim = document.getElementById("listing_delimiter").value;
	var filename = delatnost.replace(/\s+/g,"_") + ".txt";
	filename = filename.replace(/\(|\)/g,"");
	filename = filename.replace(/&/g,"[AND]");
	var trs = table.getElementsByTagName("tr");
	var out = "";
	
	var srodne_delatnosti = document.getElementById("srodne_delatnosti").value;
	var links_names = document.getElementById("links_names").value;
	var links_adrese = document.getElementById("links_adrese").value;
	var links_tels = document.getElementById("links_tels").value;
	
	for(var i=0; i<trs.length; i++){
		if(trs[i].className != "head"){
			var tds = trs[i].getElementsByTagName("td");
			out += "{!!}";
			out += "name="+_escape(trim(tds[1].innerHTML))+"{!}";
			out += "mesto="+_escape(trim(tds[2].innerHTML))+"{!}";
			out += "adresa="+_escape(trim(tds[3].innerHTML))+"{!}";
			out += "telefon="+_escape(trim(tds[4].innerHTML));
			/*out += "name="+(trim(tds[1].innerHTML))+"{!}";
			out += "mesto="+(trim(tds[2].innerHTML))+"{!}";
			out += "adresa="+(trim(tds[3].innerHTML))+"{!}";
			out += "telefon="+(trim(tds[4].innerHTML));*/
		}
	}
	out = out.substr(4);
	a = "a=listing"
	a+= "&mod=makeTXT"
	a+= "&delid="+delid
	a+= "&srodne_delatnosti="+escape(srodne_delatnosti)
	a+= "&links_names="+escape(links_names)
	a+= "&links_adrese="+escape(links_adrese)
	a+= "&links_tels="+escape(links_tels)
	a+= "&data="+out
	a+= "&delim="+escape(delim);
	LoadRSDoc("komitenti.php","afterExportListingTXT('"+filename+"')",a,"",1);
}
function afterExportListingTXT(response,filename){
	var tmp = response.split("{!}",2);
	var testHTML = tmp[0];
	var file = tmp[1];
	var html = '<div class="area">';
	//html += "<pre>"+response+"</pre>";
	html += '<a class="download" href="down.php?f='+file+'&fn='+filename+'" onclick="closeYpAjaxForm()" style="display:none;">preuzmite fajl...</a>';
	html += testHTML;
	html += '</div>';
	ypAjaxForm(html);
}
function exportListingTXTCompareTables(btn){
	var table1 = document.getElementById("listingDelatnostiTable");
	var table2 = document.getElementById("listingTestTable");
	var downloadBTN = btn.previousSibling;
	
	var t1 = getTableContents(table1,1,1,1);
	var t2 = getTableContents(table2,1,0);
	//ypAjaxForm('<textarea>'+t1+'</textarea><textarea>'+t2+'</textarea>');
	if(t1 != t2){
		if(!confirm("Podaci nisu identični!\nDa li ipak želite da preuzmete listing.")){
			return;
		}
	}else{
		alert("Podaci su identični.");
	}
		
	btn.style.color = "#000";
	downloadBTN.style.display="inline";
}

function getTableContents(tbl,offsetRow,offsetCol,yuscii){
	yuscii = yuscii>0?true:false;
	
	offsetRow = offsetRow>0?offsetRow:0;
	offsetCol = offsetCol>0?offsetCol:0;
	
	var trs = tbl.getElementsByTagName("tr");
	var ret = "";
	for(var i=offsetRow; i<trs.length; i++){
		var tds = trs[i].getElementsByTagName("td");
		for(var j=offsetCol; j<tds.length; j++){
			var txt = trim(tds[j].innerHTML);
			if(yuscii){
				txt = utf2yuscii(txt,1)
			}
			ret += escape(txt)+"{!}";
		}
		ret += "{!!}";
	}
	return ret;
}
function updateSearchReportTM(response){
	document.getElementById("searchReportTMContent").innerHTML = response;
}
function AddKomitentBrand(olid,brandid,brandname){
	var list = document.getElementById(olid);
	if(list){
		var lis = list.getElementsByTagName("li");
		var ima=false;
		if(lis.length){
			for(var i=0; i<lis.length; i++){
				if(lis[i].id==brandid){
					ima=true;
					break;
				}
			}
		}
		if(!ima){
			var li = document.createElement("li");
			li.id=brandid;
			li.innerHTML = brandname;
			list.appendChild(li);
			li.onclick = function(){
				this.className = this.className.length>0?"":"selected";
			}
		}
	}
}
function deleteKomitentiBrands(olid){
	var list = document.getElementById(olid);
	if(list){
		var lis = document.getElementsByTagName("li");
		for(var i=0; i<lis.length; i++){
			if(lis[i].className=="selected"){
				list.removeChild(lis[i]);
				i--;
			}
		}
	}
}
function afterKomitentMaterijalDelete(response){
	var tmp = response.split("{!}",2);
	var code = parseInt(tmp[0]);
	var msg = tmp[1];
	alert(msg);
	if(!code){
		LoadRSDoc('komitenti.php?a=materijal','setContent');
	}
}